import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../widgets/CommonWidget.dart';
import '../../widgets/LoadStateWidget.dart';
import '../logic/BasicListLogic.dart';
import 'BasePage.dart';
import 'BasicPage.dart';

///BasicListPage 列表页基类, 抽取渲染页面公共部分
abstract class BasicListPage<Page extends BasePage, T extends BasicListLogic>
    extends BasicPage<Page, T> {
  @override
  Widget body(BuildContext context) {
    return GetBuilder<T>(
      init: currentLogic,
      builder: (_) {
        final loadStateLayout = LoadStateLayout(
          errorRetry: currentLogic.configRefresh,
          type: emptyType() ?? EmptyType.Empty_Search,
          state: currentLogic.pageState,
          emptyWidget: emptyWidget(),
          successWidget: youthRefresher(
            refreshController: currentLogic.refreshController,
            child: listView(),
            onRefresh: currentLogic.configRefresh,
            onRetry: () {
              currentLogic.footerRetryRefresh();
            },
            onLoading: currentLogic.configLoading,
          ),
        );
        return Column(
          children: [
            listHeaderView() ?? Container(),
            Expanded(
              child: loadStateLayout,
            ),
            footView() ?? Container(),
          ],
        );
      },
    );
  }

  /// NOTE: list列表页配置
  /// 自定义在列表底部添加布局，子类可选实现
  Widget? footView() {
    return null;
  }

  Widget? listHeaderView() {
    return null;
  }

  Widget listView() {
    return ListView.separated(
      itemBuilder: (c, i) => listItem(i),
      itemCount: currentLogic.dataList.length,
      controller: currentLogic.controller,
      separatorBuilder: (context, index) => Container(),
    );
  }

  EmptyType? emptyType() {
    return null;
  }

  Widget listItem(int i);

  /// NOTE: list列表页配置
  /// 自定义空数据页，子类可选实现
  Widget? emptyWidget() {
    return null;
  }
}
